﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;

namespace 序列化和反序列化
{
	class Program
	{
		static void Main(string[] args)
		{
			///要将P这个对象传送到电脑桌面
			//Person p = new Person();
			//p.Name = "小明";
			//p.Age = 12;
			//p.Gender = "男";
			//using (FileStream fsWrite = new FileStream(@"C:\Users\zcy\Desktop\111.txt", FileMode.OpenOrCreate, FileAccess.Write))
			//{
			//	BinaryFormatter binary = new BinaryFormatter();
			//	binary.Serialize(fsWrite, p);
			//}
			//Console.WriteLine("序列化成功");
			//Console.ReadKey();

			///接受传输过来的二进制数据，反序列化成对象
			///
			Person person;
			using (FileStream fsRead = new FileStream(@"C:\Users\zcy\Desktop\111.txt", FileMode.OpenOrCreate, FileAccess.Read))
			{
				BinaryFormatter binary = new BinaryFormatter();
				person = (Person)binary.Deserialize(fsRead);
			}
			Console.WriteLine(person.Name);
			Console.WriteLine(person.Age);
			Console.WriteLine(person.Gender);
			Console.ReadKey();
		}
	}
	[Serializable]
	public class Person
	{
		public int ID { get; set; }
		public string Name { get; set; }
		public int Age { get; set; }
		public string Gender { get; set; }
	}
}
